package gjj.gui;

import gjj.util.ImageUtils;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.io.File;

public class ImagePreviewer extends OpenCVSupportedGui {

    private JLabel imageLabel;

    private byte[] readImage(String filePath) {
        Mat image = Imgcodecs.imread(filePath, Imgcodecs.IMREAD_UNCHANGED);
        ImageUtils.fitImgSize(image, image, IMAGE_WIDTH, IMAGE_HEIGHT);
        MatOfByte buffer = new MatOfByte();
        Imgcodecs.imencode(".jpeg", image, buffer);
        return buffer.toArray();
    }

    protected void init() {
        this.setTitle("图片展示工具");
        //vBox放置上下分布的图像显示区域和下面的操作按钮
        Box vBox = Box.createVerticalBox();
        //上面图像显示区域
        imageLabel = new JLabel();
        imageLabel.setBorder(new LineBorder(Color.BLUE, 2));
        imageLabel.setPreferredSize(new Dimension(IMAGE_WIDTH, IMAGE_HEIGHT));
        imageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        JPanel imagePanel = new JPanel();
        imagePanel.setLayout(new FlowLayout());
        imagePanel.add(imageLabel);
        vBox.add(imagePanel);
        //下面放置选择文件的操作按钮
        JFileChooser fileChooser  = new JFileChooser();
        fileChooser.setDialogTitle("选择图片文件预览");
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.setCurrentDirectory(new File("/Users/kwok/Downloads/"));
        fileChooser.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                if (null == f) return false;
                String suffix = f.getName().substring(f.getName().lastIndexOf(".") + 1);
                if ("jpeg".equalsIgnoreCase(suffix) || "jpg".equalsIgnoreCase(suffix) || "png".equalsIgnoreCase(suffix) || "tiff".equalsIgnoreCase(suffix) || "tif".equalsIgnoreCase(suffix)) {
                    return true;
                }
                return false;
            }
            @Override
            public String getDescription() {
                return "支持jpg、jpeg、png、tif、tiff";
            }
        });
        JButton chooseFileBtn = new JButton("选择文件");
        chooseFileBtn.addActionListener(e -> {
            int retValue = fileChooser.showDialog(null, "打开图片");
            if (retValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                imageLabel.setIcon(new ImageIcon(readImage(selectedFile.getAbsolutePath())));
            } else {
                System.out.println("用户取消选中文件");
            }
        });
        JPanel menuBar = new JPanel();
        menuBar.setLayout(new FlowLayout());
        menuBar.setBorder(new LineBorder(Color.RED, 2));
        menuBar.add(chooseFileBtn);
        vBox.add(menuBar);
        //把vBox加入到应用容器中
        this.add(vBox);
    }

    public static void main(String[] args) {
        ImagePreviewer app = new ImagePreviewer();
        app.setVisible(true);
    }
}
